<?php

namespace app\admin\controller;

use think\facade\Session;
use app\admin\controller\Base;
use app\common\model\Category as CategoryModel;

class CategoryRoot extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->Model = new CategoryModel();
        $this->parent_id = 0;
    }

    public function index(){
      $list = $this->Model->childrenSelect(['parent_id' => $this->parent_id, 'select_with' => 'pc_banner,member', 'order_type' => 'id_asc']);
      $this->assign('list', $list);
      return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        // 用户不能创建一级栏目分类
        $this->redirect(url('index', ['parent_id' => $this->parent_id]));
        // if($this->request->isPost()){
        //     $data = $this->getPostData();
        //     $res = $this->Model->createOrUpdate($data);
        //     if($res["status"]){
        //         Session::flash('success', $res['msg']);
        //         $this->success($res["msg"], url('index', ['parent_id' => $this->parent_id]));
        //     }else{
        //         Session::flash('danger', $res['msg']);
        //         $this->error($res["msg"]);
        //     }
        // }else{
        //     $category = ["status" => 1, "pc_cover_id" => 0, "sort" => 0];
        //     $this->assign("category", $category);
        //     return $this->fetch("form");
        // }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id = 0)
    {
        $id = intval($id);
        $category = $this->Model->where("id", $id)->where("parent_id", $this->parent_id)->find();
        if(empty($category)){
            Session::flash('danger', '未找到编辑记录');
            $this->redirect('index');
        }

        if($this->request->isPost()){
          $data = $this->getPostData();
          $res = $this->Model->createOrUpdate($data);
          if($res["status"]){
            Session::flash('success', $res['msg']);
            $this->success($res["msg"], url('index', ['parent_id' => $this->parent_id]));
          }else{
            Session::flash('danger', $res['msg']);
            $this->error($res["msg"]);
          }
        }else{
          $this->assign("category", $category);
          return $this->fetch("form");
        }
    }

    public function delete($id = 0){
        // 用户不能删除一级栏目分类
        if($this->request->isDelete()){
            $this->error('您请求的方法不存在', url('index', ['parent_id' => $this->parent_id]));
        }else{
            $this->redirect(url('index', ['parent_id' => $this->parent_id]));
        }
    }

    public function resort(){
        $this->redirect(url('index', ['parent_id' => $this->parent_id]));
    }

    private function getPostData(){
        $fields = [
            ['name' => 'id', 'type' => 'integer', 'is_null' => true],
            ['name' => 'pc_banner_id', 'type' => 'integer', 'default' => 0],
            ['name' => 'title', 'type' => 'string', 'default' => ''],
            ['name' => 'name', 'type' => 'string', 'default' => ''],
        ];

        $default = [
            'parent_id' => $this->parent_id,
            'wap_banner_id' => 0,
            'status' => 1,
        ];

        return $this->parsePostParam($fields, $default);
    }
}
